#include "os_mutex.h"


extern int os_mutex_init(os_mutex_st OS_MUTEX_MEMORY * const mutex)
{
    if(NULL == mutex)
    {
        return -2; // Invalid mutex pointer
    }

    mutex->holder = 0; // Initialize mutex as not held
    return 0; // Success
}

int os_mutex_trylock(os_mutex_st OS_MUTEX_MEMORY * const mutex)
{
    if(NULL == mutex)
    {
        return -2; // Invalid mutex pointer
    }

    if (mutex->holder == 0) // If mutex is not held
    {
        mutex->holder = 1 ;
        return 0; // Success
    }
    return -1; // Mutex already held
}

int os_mutex_unlock(os_mutex_st OS_MUTEX_MEMORY * const mutex)
{
    if(NULL == mutex)
    {
        return -2; // Invalid mutex pointer, do nothing
    }
    mutex->holder = 0; // Release the mutex

    return 0;
}
